package com.fm.app03.Service.float_service;

import static com.fm.app03.Service.float_service.MyAccessibilityService.myAccessibilityService;
import static com.fm.app03.Service.float_service.MyViewStateListener.shouzhi_X;
import static com.fm.app03.Service.float_service.MyViewStateListener.shouzhi_y;
import static com.fm.app03.os.MyApplication.instance;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.accessibility.AccessibilityManager;
import android.view.animation.AccelerateInterpolator;
import android.widget.Button;

import com.fm.app03.R;
import com.yhao.floatwindow.FloatWindow;
import com.yhao.floatwindow.MoveType;
import com.yhao.floatwindow.Screen;

public class FloatingWidgetService{

    public static String TAG = "FloatingWidgetService";

    public static  View Float_view;
    public static  View Float_click_view;

    private static Boolean IsShow = false;
    private static Boolean IsClicking = false;



    private static Handler handler;
    private static Runnable runnable;
    private static Handler handler0;
    private static Runnable runnable0;

    /*************************************************************************/

    /**
     * 点击定时任务
     */
    public static void startTimer() {
        handler = new Handler();
        runnable = new Runnable() {
            @Override
            public void run() {
                // 在此处执行点击操作
                myAccessibilityService.clickAtPoint(shouzhi_X, shouzhi_y, 10);
                Log.i(TAG, "run: 11111111111111111111111");
                // 每秒执行一次
                handler.postDelayed(this, 100);
            }
        };

        // 1000秒后 启动定时器
        handler.postDelayed(runnable, 1000);
    }

    public static void stopTimer() {
        // 停止定时器
        handler.removeCallbacks(runnable);
    }

    /**
     * 滑动定时任务
     */
    public static void startTimer0() {
        handler0 = new Handler();
        runnable0 = new Runnable() {
            @Override
            public void run() {
                // 在此处执行点击操作
                myAccessibilityService.performSwipe();
                Log.i(TAG, "run: 0000000000");
                // 每秒执行一次
                handler0.postDelayed(this, 3000);
            }
        };

        // 1000秒后 启动定时器
        handler0.postDelayed(runnable0, 1000);
    }

    public static void stopTimer0() {
        // 停止定时器
        handler0.removeCallbacks(runnable0);
    }





    /*************************************************************************/


    /**
     * 判断是否具有无障碍权限-服务是否开启
     * @param context
     */
    public static void openAccessibilitySettingsIfNecessary(Context context) {
        AccessibilityManager accessibilityManager = (AccessibilityManager) context.getSystemService(Context.ACCESSIBILITY_SERVICE);

        boolean isAccessibilityEnabled = accessibilityManager.isEnabled();
        Log.i("TAG", "openAccessibilitySettingsIfNecessary: " + accessibilityManager + isAccessibilityEnabled);
        if (!isAccessibilityEnabled) {
            Intent intent = new Intent(android.provider.Settings.ACTION_ACCESSIBILITY_SETTINGS);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
    }

    /**
     * 悬浮框控制器
     * @param context
     */
    public static void float_window(Context context) {
        /*是否开启悬浮权限*/
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(context.getApplicationContext())) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + context.getPackageName()));
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }
        /*判断是否具有无障碍权限*/
        openAccessibilitySettingsIfNecessary(context.getApplicationContext());
        /*制作悬浮框*/
        Float_view = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.float_window, null);
        /*监听悬浮框里面的按钮*/
        Button slide_bt_1 = Float_view.findViewById(R.id.switch_button0);
        slide_bt_1.setOnClickListener(new View.OnClickListener() {
            Boolean IsClick = false;
            @Override
            public void onClick(View v) {
               //TODO  滑动
                if(IsClicking){
                    stopTimer0();
                    IsClicking = false;
                    slide_bt_1.setText("一直滑动");
                }else {
                    startTimer0();
                    IsClicking = true;
                    slide_bt_1.setText("滑动中...");
                }

            }
        });

        Button slide_bt_2 = Float_view.findViewById(R.id.switch_button1);


        slide_bt_2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO  一直点击
                if(IsClicking){
                    stopTimer();
                    IsClicking = false;
                    slide_bt_2.setText("一直点击");
                }else {
                    startTimer();
                    IsClicking = true;
                    slide_bt_2.setText("点击中...");
                }
                Log.i(TAG, "onClick: "+shouzhi_X+shouzhi_y);


            }
        });

        View slide_bt_3 = Float_view.findViewById(R.id.switch_button2);
        slide_bt_3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO  显示位移手指
                if(IsShow){
                    FloatWindow.destroy("new_0");
                    IsShow = false;
                }else {
                    float_click_window(instance);
                    IsShow = true;
                }

            }
        });




        /*创建悬浮窗*/
        FloatWindow
                .with(context.getApplicationContext())
                .setView(Float_view)
                .setX(10)                                   //设置控件初始位置
                .setY(Screen.height, 0.3f)
                .setDesktopShow(true)                               //桌面显示
                .setMoveType(MoveType.slide)
                .setMoveStyle(500, new AccelerateInterpolator())  //贴边动画时长为500ms，加速插值器
                .setTag("new")
                .build();
        FloatWindow.get("new").show();
    }


    /**
     * 点击悬浮窗
     * @param context
     */
    public static void float_click_window(Context context){
        Log.i(TAG, "float_click_window: ----------------------------------------------------------------");

        Float_click_view = LayoutInflater.from(context.getApplicationContext()).inflate(R.layout.float_click_window, null);

        /*创建悬浮窗*/
        FloatWindow
                .with(context.getApplicationContext())
                .setView(Float_click_view)
                .setX(shouzhi_X)                   // 设置 X 坐标为屏幕宽度的百分之百
                .setY(shouzhi_y)
                .setDesktopShow(true)                               //桌面显示
                .setViewStateListener(new MyViewStateListener())    //监听悬浮控件状态改变
                .setPermissionListener(new MyPermissionListener())  //监听权限申请结果
                .setMoveType(MoveType.active)
                .setTag("new_0")
                .build();

        FloatWindow.get("new_0").show();
    }



}
